package com.iss.controller.user;

import com.google.gson.Gson;
import com.iss.model.ResponseMessage;
import com.iss.model.User;
import com.iss.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/user/save")
public class SaveController extends HttpServlet {
   private UserService service = new UserService();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Gson gson = new Gson();
        ResponseMessage responseMessage = new ResponseMessage();
       Boolean flag=false;

        String username = req.getParameter("username");
        String password=req.getParameter("password");
        String userId = req.getParameter("userId");

        User user = new User();
        if (!userId.equals("")){
            user.setUserId(Integer.parseInt(userId));
        }
        user.setUsername(username);
        user.setPassword(password);

         flag = service.save(user);
         responseMessage.setFlag(flag);

         resp.getWriter().println(gson.toJson(responseMessage));


    }
}
